package com.library.config;

import cn.dev33.satoken.stp.StpInterface;
import com.library.entity.User;
import com.library.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Sa-Token 权限验证接口实现
 */
@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {
    
    private final UserService userService;
    
    /**
     * 返回指定账号id所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 本系统暂不使用权限码，返回空集合
        return new ArrayList<>();
    }
    
    /**
     * 返回指定账号id所拥有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roles = new ArrayList<>();
        
        try {
            // 根据用户ID获取用户信息
            Long userId = Long.parseLong(loginId.toString());
            User user = userService.getById(userId);
            
            if (user != null && user.getRole() != null) {
                roles.add(user.getRole());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return roles;
    }
}

